package MapReduce.Demo4_MRCompress;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.GzipCodec;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @Author lixin
 * @Date 2023/3/15 10:56
 */
public class WordCountCompressApp {

    public static class WCMapper extends Mapper<LongWritable, Text,Text, IntWritable>{

        Text outKey = new Text();
        IntWritable outValue = new IntWritable(1);

        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {

            String line = value.toString();
            String[] words = line.split(" ");

            for (String word : words) {
                outKey.set(word);
                context.write(outKey,outValue);
            }

        }
    }

//    public static class WCReducer extends Reducer<Text,IntWritable,Text,IntWritable> {
//
//        IntWritable outValue = new IntWritable();
//
//        @Override
//        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
//
//            int sum = 0;
//
//            for (IntWritable value : values) {
//                sum += value.get();
//            }
//
//            outValue.set(sum);
//
//            context.write(key,outValue);
//
//        }
//    }


    public static void main(String[] args) throws Exception {

        Configuration conf = new Configuration();

        //Mapper端输入可以读取的压缩格式，默认任何格式都可以读取
        conf.set("io.compression.codecs","org.apache.hadoop.io.compress.DefaultCodec," +
//                "org.apache.hadoop.io.compress.GzipCodec," +
                "org.apache.hadoop.io.compress.BZip2Codec," +
                "org.apache.hadoop.io.compress.Lz4Codec," +
                "org.apache.hadoop.io.compress.SnappyCodec");

        Job job = Job.getInstance(conf);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
//        job.setOutputKeyClass(Text.class);
//        job.setOutputValueClass(IntWritable.class);

        job.setJarByClass(WordCountCompressApp.class);
        job.setMapperClass(WCMapper.class);
//        job.setReducerClass(WCReducer.class);


        Path outputPath = new Path(args[1]);

        FileSystem fs = FileSystem.get(conf);

        if (fs.exists(outputPath)) {
            fs.delete(outputPath,true);
        }


        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,outputPath);

        //是否开启reduce端输出的压缩形式
        FileOutputFormat.setCompressOutput(job,true);
        //如果开启压缩，必须指定输出的压缩格式类型
        FileOutputFormat.setOutputCompressorClass(job, GzipCodec.class);


        job.setNumReduceTasks(0);

        job.waitForCompletion(true);

    }

}
